﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;
namespace Guoc.DAL.AOPFilter
{
    /// <summary>
    /// AOP 过滤类，要实现自己的切面，只需要继承该类即可
    /// </summary>
    public class AopFilter : IMessageSink
    {
        IMessageSink _nextMsgSink;

        public AopFilter(IMessageSink next)
        {
            this._nextMsgSink = next;
        }

        /// <summary>
        /// 异步处理消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="replySink"></param>
        /// <returns></returns>
        public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 下一个消息接受器
        /// </summary>
        public IMessageSink NextSink
        {
            get { return _nextMsgSink; }
        }

        /// <summary>
        /// 同步处理消息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public IMessage SyncProcessMessage(IMessage msg)
        {

            OnExecuting(msg);
            IMessage filterMsg = _nextMsgSink.SyncProcessMessage(msg);
            OnExecuted(filterMsg);
            return filterMsg;

        }

        /// <summary>
        /// 调用方法前执行
        /// </summary>
        /// <param name="msg"></param>
        public virtual void OnExecuting(IMessage msg)
        {
        }
        /// <summary>
        /// 调用方法后执行
        /// </summary>
        /// <param name="returnMsg"></param>
        public virtual void OnExecuted(IMessage msg)
        {
        }
    }
}
